

$(function () {
    // $("#introduce").bind('keyup blur', function () {
    //     checkServiceIntroduceMsg($(this));
    // });

    //上传文件
    $('.form-file-btn').on('click', function () {
        var id = $(this).closest('.form-item').attr('id');
        $('.form-upload-btn').attr('data-target', id).click();
    });
})


function checkServiceIntroduceMsg(validateTag) {
    var success = true;
    var value = validateTag.val();
    //请填写服务简介
    if (trim(value).length == 0) {
        success = false;
        alert("不能为空");
    }
    // 请填写至少写30个字符，不大于100个字符

    if (!(getByteLength(value) >= 30 && getByteLength(value) <= 200)) {
        success = false;
        alert("不满足要求");
    }
    return success;
}

//获取参数的长度，数字英文长度记为1，中文记为2
var getByteLength = function(value) {
    var length = 0;
    value = trim(value);
    for (var i = 0; i < value.length; i++) {
        if (value.charCodeAt(i) < 27 || value.charCodeAt(i) > 126) {
            length += 2;
        } else {
            length++;
        }
    }
    return length;
}


//文章图片上传处理
$('.form-upload-btn').on('change', function () {
    var $target = $('#' + $(this).attr('data-target'));
    var $btn = $target.find('.form-file-btn'),
        file = $(this).val().trim();
    if (file.length == 0) {
        return false;
    } else {
        var $file = $target.find('.form-file');
        $file.html('<span class="form-file-loading"></span>');
        var options = {
            type: "post",
            dataType: "json",
            success: function (data) {
                if (data.code == 0) {
                    var url = data.returnObject.url;
                    $file.html('<span class="form-file-server"><img  src="' + url + '"/></span>');
                    $target.find(".plugin-img").val(url);
                    console.log("success");
                } else if (data.code == 1) {
                    $target.find(".plugin-img").val("");
                    showError(data.enMessage, false, $("#serviceImg"));
                    console.log("aaaaaaa code = 1");
                } else {
                    $target.find(".plugin-img").val("");
                    console.log("aaaaaaa code = other");
                }
            }, error: function (data) {
                $target.find(".plugin-img").val("");
                console.log("aaaaaaa code = error");
            }, complete: function () {
                $file.find('.form-file-loading').remove();
            }
        };
        $("#formUpload").ajaxSubmit(options);
    }

});



// 发布文章
function addArticle() {

    //标题
    var title = $("#txtTitle").val();
    //文章图片
    var articleLogo = $("#serviceImg input").val();
    //文章内容
    var articleContent = $("#articleContent").val();
    //文章简介
    var introduce = $("#introduce").val();
    //文章标签
    var tagIds = $("#tagIds").val();
    //文章类型
    var selType = $("#selType").val();
    //公开度
    var articlePublic = $("input[type='radio'][name='articlePublic']:checked").val()



    $.ajax({
        type: 'POST',
        url: "/article/add.action",
        dataType: 'json',
        data: {
            "title": title,
            "articleLogo": articleLogo,
            "content": articleContent,
            "articleIntroduce": introduce,
            "tagIds": tagIds,
            "articleType": selType,
            "articlePublic": articlePublic
        },
        success: function (data) {
            if (data.code == 0) {
                alert("success");
                return;
            } else {
                alert("hehhe");
                return;
            }
        }, error: function () {
            alert("网络错误");
        }, complete: function () {

        }
    });

}